<?php

/**
 * payment actions.
 *
 * @package    sf_sandbox
 * @subpackage payment
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class paymentActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  	    $this->setDecorator();
        $c = new Criteria ( );
        $csum = new Criteria ( );
        
        /// Делаем критерий
        
        if($request->getParameter('payment_filters[payment_id]'))
        {
        	$c->addAnd(PaymentPeer::PAYMENT_ID,$request->getParameter('payment_filters[payment_id]'));
        	$csum->addAnd(PaymentPeer::PAYMENT_ID,$request->getParameter('payment_filters[payment_id]'));
        }

          if($request->getParameter('payment_filters[level_id]'))
        {
        	$c->addAnd(PaymentPeer::LEVEL_ID,$request->getParameter('payment_filters[level_id]'));
        	$csum->addAnd(PaymentPeer::LEVEL_ID,$request->getParameter('payment_filters[level_id]'));
        }
        
          if($request->getParameter('payment_filters[date_from]'))
        {
        	$c->addAnd(PaymentPeer::CREATED_AT,date('Y-m-d', strtotime($request->getParameter('payment_filters[date_from]'))),Criteria::GREATER_EQUAL);
            $csum->addAnd(PaymentPeer::CREATED_AT,date('Y-m-d', strtotime($request->getParameter('payment_filters[date_from]'))),Criteria::GREATER_EQUAL);
        }
        
            if($request->getParameter('payment_filters[date_to]'))
        {
        	$c->addAnd(PaymentPeer::CREATED_AT,date('Y-m-d', strtotime($request->getParameter('payment_filters[date_to]'))),Criteria::LESS_EQUAL);
        	$csum->addAnd(PaymentPeer::CREATED_AT,date('Y-m-d', strtotime($request->getParameter('payment_filters[date_to]'))),Criteria::LESS_EQUAL);
        }        
        
          if(!$request->getParameter('page')){
  	    	$this->page = 1;
  	    }
  	    else {
  	    	$this->page=$request->getParameter('page');
  	    } 
        
  	    $pager = new sfPropelPager ( 'Payment', sfConfig::get ( 'app_payment_perpage' ) );
		$pager->setCriteria ( $c );
		$pager->setPeerMethod ( 'doSelect' );
		$pager->setPage ( $this->page );
		$pager->init ();
		$this->payment = $pager;
		
		///Подбиваем сумму
		
		$csum->clearSelectColumns();
		$csum->addSelectColumn('SUM(' . PaymentPeer::SUMMA.')'); 
		$stmt = PaymentPeer::doSelectStmt($csum);
		$this->suma = $stmt->fetchColumn(0);
		
  	
  }
  
  
	public function setDecorator()
	{
		
		$module = $this->getRequest()->getParameter('module');
		$action = $this->getRequest()->getParameter('action');
		
		$path_layout = sfConfig::get('sf_app_template_dir').'/layouts/'.sfConfig::get('app_name').'/'.$module.'.php';
		$path_template = sfConfig::get('sf_app_module_dir').'/'.$module.'/templates/'.sfConfig::get('app_name').'/'.$action.'Success.php';
		if(file_exists($path_layout))
		{ $l = 'layouts/'.sfConfig::get('app_name').'/'.$module; }
		else
		{ $l = sfConfig::get('app_name').'_layout'; }
		if(file_exists($path_template))
		{ $t = sfConfig::get('app_name').'/'.$action; }
		else
		{ $t = $action; }
			
	/*
     * Установка layout
     */	
			$this->setLayout ( $l );
		
     /*
     *  Устанавливаем шаблон
     */
		
			$this->setTemplate ( $t );
	}

	/*
	 * ошибка оплаты
	 */
	public function executeNopay()
	{
		
	}	
	

	/*
	 * оплата произведена
	 */
	public function executeYespay(sfWebRequest $request)
	{


                switch ($request->getParameter('item1_price')) {
                case '5':
                    $summa = 10;
                    break;
                case '24':
                   $summa = 50;
                    break;
                case '47':
                    $summa = 100;
                    break;
                case '69':
                    $summa = 150;
                    break;
                case '90':
                    $summa = 200;
                    break;
                case '200':
                    $summa = 500;
                    break;
            }

                $u = sfGuardUserPeer::retrieveByPK($request->getParameter('user_id'));
                $u->setAccount($u->getAccount()+$summa);
                $u->save();

                $p = new Payment();
                $p->setUserId($request->getParameter('user_id'));
                $p->setPaymentId($request->getParameter('user_id'));
                $p->setLevelId(1);
                $p->setSumma($request->getParameter('item1_price'));
                $p->setDesc('multicadr');
                $p->save();
		$this->setLayout(false);
	}	
	
	
}
